import React, { Component } from 'react'
import store, {actionCreator} from '../store'
import { NavLink } from 'react-router-dom'

export default class Redux2 extends Component {
  constructor() {
    super()
    this.state = {
      age: 0,
      name: ''
    }
  }
  componentDidMount() {
    this.setState(store.getState());
    this.un = store.subscribe(()=>{
      this.setState(store.getState());
    })
  }
  componentWillUnmount() {
    // 取消监听
    this.un()
  }
  changeAge() {
    store.dispatch(actionCreator.ageAdd())
  }
  render() {
    return (
      <div>
        <p>名字：{this.state.name}</p>
        <p>年龄：{this.state.age}</p>
        <button onClick={()=>this.changeAge()}>过生日</button>
        <NavLink to="/redux">redux</NavLink>
      </div>
    )
  }
}
